package cn.tedu.apps_system.mapper;

import cn.tedu.apps_system.pojo.vo.ProductSalesVO;
import cn.tedu.apps_system.pojo.vo.SalesStatisticsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SalesStatisticsMapper {
    @Select("SELECT SUM(oi.quantity) AS totalSales, SUM(oi.total_price) AS totalAmount " +
            "FROM order_item oi " +
            "JOIN `order` o ON oi.order_id = o.id " +
            "WHERE o.merchant_id = #{merchantId} AND o.status = 3")
    SalesStatisticsVO selectSalesStatistics(Long merchantId);

    @Select("SELECT oi.product_id, p.name AS product_name, SUM(oi.quantity) AS sales, SUM(oi.total_price) AS amount " +
            "FROM order_item oi " +
            "JOIN `order` o ON oi.order_id = o.id " +
            "JOIN product p ON oi.product_id = p.id " +
            "WHERE o.merchant_id = #{merchantId} AND o.status = 3 " +
            "GROUP BY oi.product_id, p.name " +
            "ORDER BY sales DESC")
    List<ProductSalesVO> selectProductSales(Long merchantId);
}